import { dictionary } from "@/api/public"
// 保存用户信息
export function setToken(data) {
	try {
		let value = uni.setStorageSync('tokenData', data);
		return value
	} catch (e) {
		return ''
	}
}
// 获取用户信息
export function getToken() {
	try {
		let value = uni.getStorageSync('tokenData');
		return value
	} catch (e) {
		return ''
	}
}
// 保存用户信息
export function setUser(data) {
	try {
		let value = uni.setStorageSync('userData', data);
		return value
	} catch (e) {
		return ''
	}
}
// 获取用户信息
export function getUser() {
	try {
		let value = uni.getStorageSync('userData');
		return value
	} catch (e) {
		return ''
	}
}
// 保存 字典
export function setDictType(){
	dictionary({}).then(res =>{
		if(res.code == 200){
			uni.setStorageSync('dictionaryData', res.data);
		}
	})
	
}
// 获取指定字典
export async function getDictTypeList(dictValue) {
	let dictTypeTree = uni.getStorageSync('dictionaryData');
	if (!dictTypeTree) {
		await setDictType()
		 let dictTypeTree1 =  await uni.getStorageSync('dictionaryData');
		 if (!dictTypeTree1) {
			const tree =  await dictTypeTree1.find((item) => item.dictValue === dictValue)
			if (tree) {
				return tree.children.map((item) => {
					return {
						value: item['dictValue'],
						text: item['name']
					}
				})
			}
			return []
		}
	}else{
		const tree = dictTypeTree.find((item) => item.dictValue === dictValue)
		if (tree) {
			return tree.children.map((item) => {
				return {
					value: item['dictValue'],
					text: item['name']
				}
			})
		}
		return []
	}
	
}